package com.shengtsource.minicat.processor;

import com.shengtsource.minicat.mapper.Mapper;
import com.shengtsource.minicat.mapper.MapperContext;
import com.shengtsource.minicat.mapper.MapperHost;
import com.shengtsource.minicat.mapper.MapperWapper;
import com.shengtsource.minicat.model.HttpServlet;
import com.shengtsource.minicat.model.Request;
import com.shengtsource.minicat.model.Response;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.Map;

import static java.lang.Thread.sleep;

/**
 * 请求处理器
 */
public class RequestProcessor implements Runnable{
    private Socket socket;
    private Mapper mapper;
//    private Map<String,HttpServlet> servletMap;
//
//    public RequestProcessor(Socket socket, Map servletMap){
//        this.socket = socket;
//        this.servletMap = servletMap;
//    }
    public RequestProcessor(Socket socket,Mapper mapper){
        this.socket = socket;
        this.mapper = mapper;
    }
    @Override
    public void run() {
        try {
           dispatcherRequest(socket);
        }catch (Exception e) {
            e.printStackTrace();;
        }
    }

    /**
     * 封装请求/响应对象，根据Request找到对应的对象
     * @param socket
     * @throws Exception
     */
    private void dispatcherRequest(Socket socket) throws Exception {
        Request request = new Request(socket.getInputStream());
        System.out.println("URL: "+ request.getUrl() +",Method: " + request.getMethod());
        // 封装Response对象
        Response response = new Response(socket.getOutputStream());
        if(null != processRequest(request)) {
            HttpServlet httpServlet = processRequest(request).getHttpServlet();
            httpServlet.service(request,response);
        }else {
            //静态资源
            response.outputHtml(request.getUrl());
        }
    }

    /**
     * 处理请求
     * @param request
     * @return
     */
    private MapperWapper processRequest(Request request) {
        String url = request.getUrl();
        String host = request.getHost();
        String[] names = url.split("\\/");
        List<MapperHost> hosts = mapper.getObject();
        MapperHost curHost = null;
        for (MapperHost mapperHost : hosts) {
            if(mapperHost.getName().equals(host)) {
                curHost = mapperHost;
                break;
            }
        }

        if(curHost == null) {
            return null;
        }
        List<MapperContext> contexts = curHost.getObject();
        MapperContext curContext = null;
        for (MapperContext context : contexts) {
            if(context.getName().equals("/".concat(names[1]))) {
                curContext = context;
                break;
            }
        }
        if(curContext == null) {
            return null;
        }
        List<MapperWapper> wappers = curContext.getObject();
        for (MapperWapper wapper : wappers) {
            if(wapper.getName().equals("/".concat(names[2]))) {
                return wapper;
            }
        }

        return null;
    }

}
